//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/admin_auth_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"google.golang.org/protobuf/proto"
)

type AdminUserInfoDbItem struct {
	UserName     string `bson:"_id"`
	UserDesc     string `bson:"userDesc"`
	PubKey       string `bson:"pubKey"`
	PermInfoData []byte `bson:"permInfoData"`
	TimeStamp    int64  `bson:"timeStamp"`
}

func (item *AdminUserInfoDbItem) ToAdminUserInfo() (*admin_auth_api.AdminUserInfo, error) {
	permInfo := &admin_auth_api.AdminPermInfo{}
	err := proto.Unmarshal(item.PermInfoData, permInfo)
	if err != nil {
		return nil, err
	}
	if permInfo.FeedBackPerm == nil {
		permInfo.FeedBackPerm = &admin_auth_api.FeedBackPerm{}
	}
	return &admin_auth_api.AdminUserInfo{
		UserName: item.UserName,
		UserDesc: item.UserDesc,
		PubKey:   item.PubKey,
		PermInfo: permInfo,
	}, nil
}

type _AdminUserDao struct {
	mc *mongo.Collection
}

func newAdminUserDao(db *mongo.Database) (*_AdminUserDao, error) {
	mc := db.Collection(db_ns.DB_NS_GLOBAL + "admin_user")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"timeStamp", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_AdminUserDao{mc: mc}, nil
}

func (db *_AdminUserDao) Insert(ctx context.Context, dbItem *AdminUserInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_AdminUserDao) Exist(ctx context.Context, userName string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": userName})
	return count > 0, err
}

func (db *_AdminUserDao) Get(ctx context.Context, userName string) (*AdminUserInfoDbItem, error) {
	retItem := &AdminUserInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": userName}).Decode(retItem)
	return retItem, err
}

func (db *_AdminUserDao) Remove(ctx context.Context, userName string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": userName})
	return err
}

func (db *_AdminUserDao) Count(ctx context.Context) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{})
	return uint32(count), err
}

func (db *_AdminUserDao) List(ctx context.Context, offset, limit int64) ([]*AdminUserInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{}, options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*AdminUserInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_AdminUserDao) UpdateUserDesc(ctx context.Context, userName, userDesc string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": userName}, bson.M{"$set": bson.M{"userDesc": userDesc}})
	return err
}

func (db *_AdminUserDao) UpdatePubKey(ctx context.Context, userName, pubKey string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": userName}, bson.M{"$set": bson.M{"pubKey": pubKey}})
	return err
}

func (db *_AdminUserDao) UpdatePerm(ctx context.Context, userName string, permInfoData []byte) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": userName}, bson.M{"$set": bson.M{"permInfoData": permInfoData}})
	return err
}
